---
- name: Ansible Playbook Demo
hosts:
- localhost
tasks:
- name: test connection
ping:
- name: echo hello
shell: /bin/echo hello
- name: make sure ssh is runing
service:
name: ssh
state: started
- name: sudo apt-get install redis-server
become: true
apt:
name: redis-server
state: present
Ansible的文件非常的多,所以這篇就以上簡單的Playbook內容來介紹,讓大家有初步的了解。
以下個別介紹的單一task,你可以用ansible <host-pattern> [options]
來確認。
hosts:
- localhost
hosts:這邊的hosts會到/etc/ansible/hosts尋找。- name: test connection
ping:
ping:嘗試連結遠端主機,確認遠端主機有可用的python,成功時回傳pong. (初看ansilbe時,就有的簡單範例)ansible localhost -m ping
- name: echo hello
shell: /bin/echo hello
shell:在遠端機器執行shell指令ansible localhost -m shell -a "echo | env | grep HOME"
- name: make sure ssh is runing
service:
name: ssh
state: started
service:控制遠端機器的服務ansible localhost -m service -a "name=ssh state=started"
- name: sudo apt-get install redis
become: true
apt:
name: redis-server
state: present
apt:管理apt套件ansible localhost -m apt -a "name=redis-server state=present" -b -K
-b, --become
:ansible透過ssh連到遠端機器後,讓你「變成」另一個user(default=root)。-K, --ask-become-pass
:讓你「變成」另一個user(default=root)的密碼提示輸入。語法:ansible-playbook [options] playbook.yml [playbook2 ...]
範例:ansible-playbook playbook.yml
上圖最後在安裝redis-server失敗,它說要「輸入密碼」,那要怎麼設定(提供)
可以到/etc/ansible/hosts加上以下設定,然後再執行ansible-playbook playbook.yml
,就可以完成「演出」(play)了。
這些都跟『become』有關,小編也還在了解中,安全控管很重要,所以大家就自己深入研讀了…。
待續…